home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / gfx / nsRegion.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  8KB  |  252 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Dainis Jonitis, <Dainis_Jonitis@swh-t.lv>.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  25.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37.  
  38. #ifndef nsRegion_h__
  39. #define nsRegion_h__
  40.  
  41.  
  42. // Implementation of region.
  43. // Region is represented as circular double-linked list of nsRegion::RgnRect structures.
  44. // Rectangles in this list do not overlap and are sorted by (y, x) coordinates.
  45.  
  46. #include "nsRect.h"
  47. #include "nsPoint.h"
  48.  
  49. class NS_GFX nsRegion
  50. {
  51.   friend class nsRegionRectIterator;
  52.   friend class RgnRectMemoryAllocator;
  53.  
  54.  
  55. // Special version of nsRect structure for speed optimizations in nsRegion code.
  56. // Most important functions could be made inline and be sure that passed rectangles
  57. // will always be non-empty.
  58. // 
  59. // Do not add any new member variables to this structure! 
  60. // Otherwise it will break casts from nsRect to nsRectFast, which expect data parts to be identical.
  61.   struct nsRectFast : public nsRect
  62.   {
  63.     nsRectFast () {}      // No need to call parent constructor to set default values
  64.     nsRectFast (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) : nsRect (aX, aY, aWidth, aHeight) {}
  65.     nsRectFast (const nsRect& aRect) : nsRect (aRect) {}
  66.  
  67.     // Override nsRect methods to make them inline. Do not check for emptiness.
  68.     inline PRBool Contains (const nsRect& aRect) const;
  69.     inline PRBool Intersects (const nsRect& aRect) const;
  70.     inline PRBool IntersectRect (const nsRect& aRect1, const nsRect& aRect2);
  71.     inline void UnionRect (const nsRect& aRect1, const nsRect& aRect2);
  72.   };
  73.  
  74.  
  75.   struct RgnRect : public nsRectFast
  76.   {
  77.     RgnRect* prev;
  78.     RgnRect* next;
  79.  
  80.     RgnRect () {}                           // No need to call parent constructor to set default values
  81.     RgnRect (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) : nsRectFast (aX, aY, aWidth, aHeight) {}
  82.     RgnRect (const nsRectFast& aRect) : nsRectFast (aRect) {}
  83.  
  84.     inline void* operator new (size_t) CPP_THROW_NEW;
  85.     inline void  operator delete (void* aRect, size_t);
  86.  
  87.     RgnRect& operator = (const RgnRect& aRect)      // Do not overwrite prev/next pointers
  88.     {
  89.       x = aRect.x;
  90.       y = aRect.y;
  91.       width = aRect.width;
  92.       height = aRect.height;
  93.       return *this;
  94.     }
  95.   };
  96.  
  97.  
  98. public:
  99.   nsRegion () { Init (); }
  100.   nsRegion (const nsRect& aRect) { Init (); Copy (aRect); }
  101.   nsRegion (const nsRegion& aRegion) { Init (); Copy (aRegion); }
  102.  ~nsRegion () { SetToElements (0); }
  103.   nsRegion& operator = (const nsRect& aRect) { Copy (aRect); return *this; }
  104.   nsRegion& operator = (const nsRegion& aRegion) { Copy (aRegion); return *this; }
  105.  
  106.  
  107.   nsRegion& And  (const nsRegion& aRgn1,   const nsRegion& aRgn2);
  108.   nsRegion& And  (const nsRegion& aRegion, const nsRect& aRect);
  109.   nsRegion& And  (const nsRect& aRect, const nsRegion& aRegion)
  110.   {
  111.     return  And  (aRegion, aRect);
  112.   }
  113.   nsRegion& And  (const nsRect& aRect1, const nsRect& aRect2)
  114.   {
  115.     nsRect TmpRect;
  116.  
  117.     TmpRect.IntersectRect (aRect1, aRect2);
  118.     return Copy (TmpRect);
  119.   }
  120.  
  121.   nsRegion& Or   (const nsRegion& aRgn1,   const nsRegion& aRgn2);
  122.   nsRegion& Or   (const nsRegion& aRegion, const nsRect& aRect);
  123.   nsRegion& Or   (const nsRect& aRect, const nsRegion& aRegion)
  124.   {
  125.     return  Or   (aRegion, aRect);
  126.   }
  127.   nsRegion& Or   (const nsRect& aRect1, const nsRect& aRect2)
  128.   {
  129.     Copy (aRect1);
  130.     return Or (*this, aRect2);
  131.   }
  132.  
  133.   nsRegion& Xor  (const nsRegion& aRgn1,   const nsRegion& aRgn2);
  134.   nsRegion& Xor  (const nsRegion& aRegion, const nsRect& aRect);
  135.   nsRegion& Xor  (const nsRect& aRect, const nsRegion& aRegion)
  136.   {
  137.     return  Xor  (aRegion, aRect);
  138.   }
  139.   nsRegion& Xor  (const nsRect& aRect1, const nsRect& aRect2)
  140.   {
  141.     Copy (aRect1);
  142.     return Xor (*this, aRect2);
  143.   }
  144.  
  145.   nsRegion& Sub  (const nsRegion& aRgn1,   const nsRegion& aRgn2);
  146.   nsRegion& Sub  (const nsRegion& aRegion, const nsRect& aRect);
  147.   nsRegion& Sub  (const nsRect& aRect, const nsRegion& aRegion)
  148.   {
  149.     return Sub (nsRegion (aRect), aRegion);
  150.   }
  151.   nsRegion& Sub  (const nsRect& aRect1, const nsRect& aRect2)
  152.   {
  153.     Copy (aRect1);
  154.     return Sub (*this, aRect2);
  155.   }
  156.  
  157.  
  158.   void MoveBy (PRInt32 aXOffset, PRInt32 aYOffset)
  159.   {
  160.     MoveBy (nsPoint (aXOffset, aYOffset));
  161.   }
  162.   void MoveBy (nsPoint aPt);
  163.   void SetEmpty ()
  164.   {
  165.     SetToElements (0);
  166.     mBoundRect.SetRect (0, 0, 0, 0);
  167.   }
  168.  
  169.   PRBool IsEmpty () const { return mRectCount == 0; }
  170.   PRBool IsComplex () const { return mRectCount > 1; }
  171.   PRBool IsEqual (const nsRegion& aRegion) const;
  172.   PRUint32 GetNumRects () const { return mRectCount; }
  173.   const nsRect& GetBounds () const { return mBoundRect; }
  174.  
  175.   /**
  176.    * Make sure the region has at most aMaxRects by adding area to it
  177.    * if necessary. The simplified region will be a superset of the
  178.    * original region. The simplified region's bounding box will be
  179.    * the same as for the current region.
  180.    */
  181.   void SimplifyOutward (PRUint32 aMaxRects);
  182.   /**
  183.    * Make sure the region has at most aMaxRects by removing area from
  184.    * it if necessary. The simplified region will be a subset of the
  185.    * original region.
  186.    */
  187.   void SimplifyInward (PRUint32 aMaxRects);
  188.  
  189. private:
  190.   PRUint32    mRectCount;
  191.   RgnRect*    mCurRect;
  192.   RgnRect     mRectListHead;
  193.   nsRectFast  mBoundRect;
  194.  
  195.   void Init ();
  196.   nsRegion& Copy (const nsRegion& aRegion);
  197.   nsRegion& Copy (const nsRect& aRect);
  198.   void InsertBefore (RgnRect* aNewRect, RgnRect* aRelativeRect);
  199.   void InsertAfter (RgnRect* aNewRect, RgnRect* aRelativeRect);
  200.   void SetToElements (PRUint32 aCount);
  201.   RgnRect* Remove (RgnRect* aRect);
  202.   void InsertInPlace (RgnRect* aRect, PRBool aOptimizeOnFly = PR_FALSE);
  203.   inline void SaveLinkChain ();
  204.   inline void RestoreLinkChain ();
  205.   void Optimize ();
  206.   void SubRegion (const nsRegion& aRegion, nsRegion& aResult) const;
  207.   void SubRect (const nsRectFast& aRect, nsRegion& aResult, nsRegion& aCompleted) const;
  208.   void SubRect (const nsRectFast& aRect, nsRegion& aResult) const
  209.   {    SubRect (aRect, aResult, aResult);  }
  210.   void Merge (const nsRegion& aRgn1, const nsRegion& aRgn2);
  211.   void MoveInto (nsRegion& aDestRegion, const RgnRect* aStartRect);
  212.   void MoveInto (nsRegion& aDestRegion)
  213.   {    MoveInto (aDestRegion, mRectListHead.next);  }
  214. };
  215.  
  216.  
  217.  
  218. // Allow read-only access to region rectangles by iterating the list
  219.  
  220. class NS_GFX nsRegionRectIterator
  221. {
  222.   const nsRegion*  mRegion;
  223.   const nsRegion::RgnRect* mCurPtr;
  224.  
  225. public:
  226.   nsRegionRectIterator (const nsRegion& aRegion)
  227.   {
  228.     mRegion = &aRegion;
  229.     mCurPtr = &aRegion.mRectListHead;
  230.   }
  231.  
  232.   const nsRect* Next ()
  233.   {
  234.     mCurPtr = mCurPtr->next;
  235.     return (mCurPtr != &mRegion->mRectListHead) ? mCurPtr : nsnull;
  236.   }
  237.  
  238.   const nsRect* Prev ()
  239.   {
  240.     mCurPtr = mCurPtr->prev;
  241.     return (mCurPtr != &mRegion->mRectListHead) ? mCurPtr : nsnull;
  242.   }
  243.  
  244.   void Reset ()
  245.   {
  246.     mCurPtr = &mRegion->mRectListHead;
  247.   }
  248. };
  249.  
  250.  
  251. #endif
  252.